import {
    addrInfoList,
    getInvoiceInfoList,
    getInvoiceList,
    searchInvoiceOrderList,
    deleteInvoince,
    reapplyInvoince
} from "../../../../api"
import Pagination from "../../../../components/Pagination"

/**
 * 用户中心发票--发票首页
 */
export default {
    name: "invoice-home",
    components: {Pagination},
    data() {
        return {
            testText: "sdsdsdsdsddsdsds",
            realsumPrices: 0,
            sectionDate: null,
            defaultInvoiceInfo: {},
            defaultAddr: {},
            invoiceData: [],//发票数据
            deleteDialogVisible: false,//删除弹窗
            logisticsDialogVisible: false,//物流信息弹窗
            rejectReasonDialogVisible: false,//驳回原因弹窗
            rejectReason: '',//驳回原因
            page: 1,
            size: 10,
            total: 0,
            deleteInvoiceID: '', // 删除发票ID
        }
    },
    created() {
        this.searchOrderList()
        this.searchInvoiceList()
        this.searchInvoiceInfo()
        this.searchAddr()
    },
    methods: {
        //查询可索取发票的订单
        searchOrderList() {
            searchInvoiceOrderList(this.addrInfo).then(res => {
                if (res.data.code === 200) {
                    res.data.result.forEach(item => {
                        this.realsumPrices += item.realsumPrice
                    })
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("查询开票订单失败！")
            })
        },
        //查询默认发票信息
        searchInvoiceInfo() {
            let param = {
                status: 1
            }
            getInvoiceInfoList(param).then(res => {
                if (res.data.code === 200) {
                    if (res.data.result.length > 0){
                        this.defaultInvoiceInfo = res.data.result[0]
                    }
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取列表失败！")
            })
        },
        //查询默认收货地址
        searchAddr() {
            let param = {
                status: 1
            }
            addrInfoList(param).then(res => {
                if (res.data.code === 200) {
                    if (res.data.result.length > 0) {
                        this.defaultAddr = res.data.result[0]
                    }
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取收货地址失败！")
            })
        },
        //查询发票记录
        searchInvoiceList() {
            let param = {
                timeOfApplication: this.sectionDate,
                pageNum: this.page,
                pageSize: this.size
            }
            getInvoiceList(param).then(res => {
                if (res.data.code === 200) {
                    let result = res.data.result
                    this.total = result.total
                    this.invoiceData = result.list
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取列表失败！")
            })
        },

        //分页change
        handlePageChange(data) {
            console.log(data)
            this.page = data.pageIndex
            this.size = data.pageSize
            this.searchInvoiceList()//这是重新获取数据的方法
        },
        //获取发票状态类型
        getStatusLabel(status) {
            let s = ''
            switch (status) {
                case 1:
                    s = '待审核'
                    break
                case 2:
                    s = '已驳回'
                    break
                case 3:
                    s = '已通过'
                    break
            }
            return s
        },
        //查询日期change
        searchDateChange() {
            this.page = 1
            this.size = 10
            this.searchInvoiceList()
        },

        //显示删除弹窗
        showDeleteDialog(id) {
            this.deleteDialogVisible = true
            this.deleteInvoiceID = id
        },

        // 删除发票请求
        handleDeleteInvoice() {
            this.deleteDialogVisible = false
            const loading = this.$loading({
                lock: true,
                text: '正在删除...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            deleteInvoince({
                id:this.deleteInvoiceID
            }).then(res => {
                if (res.data.code === 200) {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                    this.searchInvoiceList()
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
                loading.close()
            }).catch(err => {
                this.$message.error('删除失败')
                loading.close()
            })
        },

        // 重新申请
        handleReapply(id){
            const loading = this.$loading({
                lock: true,
                text: '正在申请中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            reapplyInvoince({
                id
            }).then(res=>{
                if (res.data.code === 200) {
                    this.$message({
                        message: '申请成功',
                        type: 'success'
                    })
                    this.searchInvoiceList()
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
                loading.close()
            }).catch(error => {
                loading.close()
                this.$message.error('重新申请失败')
            })
        },

        //头部各功能点击事件
        changeNodule(index) {
            this.$emit('moduleIndexChange', index)
        }
    }
}
